package com.ygqh.baby.shiro.realm;

import com.ygqh.baby.po.AdminPermission;
import com.ygqh.baby.po.AdminRole;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.service.AdminUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 */
public class UserRealm extends AuthorizingRealm {

	@Autowired
	private AdminUserService userService;

	/**
	 * 授权信息
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		/*AdminUser user = (AdminUser) principals.getPrimaryPrincipal();

		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		
		*//*List<AdminPermission> menus = userService.findMenuByUser(user.getId());
		user.setMenus(menus);*//*
		//目前到菜单级别，权限查询先去掉
		*//*List<AdminPermission> permissions = userService.findPermissionByUser(user.getId());
		user.setPermissions(permissions);
		
		for (AdminPermission p : permissions) {
			authorizationInfo.addStringPermission(p.getPercode());
		}*//*
		
		for (AdminPermission p : user.getMenus()) {
			authorizationInfo.addStringPermission(p.getUrl());
		}
		
		List<AdminRole> roles =  user.getRoles();
		for(AdminRole r:roles) {
			authorizationInfo.addRole(r.getName());
		}
		
		return authorizationInfo;*/
		return null;
	}

	/**
	 * 身份认证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

		String username = (String) token.getPrincipal();

		AdminUser user = userService.findByUserName(username);

		if (user == null) {
			throw new UnknownAccountException();// 没找到帐号
		}

		List<AdminPermission> menus = userService.findMenuByUser(user.getId());
		user.setMenus(menus);
		//目前到菜单级别，权限查询先去掉
		/*List<AdminPermission> permissions = userService.findPermissionByUser(user.getId());
		user.setPermissions(permissions);*/
		
		List<AdminRole> roles =  userService.findRolesByUser(user.getId());
		user.setRoles(roles);
		SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user, // 用户名
				user.getPassword(), // 密码
				ByteSource.Util.bytes(user.getCredentialsSalt()), // salt=username+salt
				getName() // realm name
		);
		return authenticationInfo;
	}

	@Override
	public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
		super.clearCachedAuthorizationInfo(principals);
	}

	@Override
	public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
		super.clearCachedAuthenticationInfo(principals);
	}

	@Override
	public void clearCache(PrincipalCollection principals) {
		super.clearCache(principals);
	}

	public void clearAllCachedAuthorizationInfo() {
		getAuthorizationCache().clear();
	}

	public void clearAllCachedAuthenticationInfo() {
		getAuthenticationCache().clear();
	}

	public void clearAllCache() {
		clearAllCachedAuthenticationInfo();
		clearAllCachedAuthorizationInfo();
	}

}
